Skip to content

Conversation

@jgray-19
Copy link
Contributor

@jgray-19 jgray-19 commented Nov 3, 2025

I apologise sincerely for the mess of this PR, I left my ruff on... Before you start to read the code, I insist that you read below, and consider the method before scrutinising the code.

Making the MAD-X pathing robust (closes #552)

I did not like the approach of setting the model dir to Path() and getting the MAD-X script and then resetting back. This approach caused bugs and was prone to developer error.

Instead, I have now added a cwd argument to wherever MAD-X scripts are created. This can be used with a function in the abstract base class _madx_path.

Instead I have created a _madx_path function that handles creating the correct paths to send to MAD-X, assuming it will be run in the model_dir.

_madx_path function

This function first checks if the path is absolute, and passes it to MAD-X as an absolute path.

Otherwise, it assumes that you would like to use the path as a relative path to the cwd, which in all cases is the model_dir, but is nonetheless flexible.

This function tries to create a relative path to the model dir using the Path.relative_to function, otherwise creates an absolute path. This in-built function will error if the path is outside of the model_dir.

Result

This means that in all cases, we are always using the actual path to the file. In the case of #552, the file path actually exists and is not just relative, should include the file if it exists.

Tests

I have not written any new tests. There is two situations in which the tests should fail before this PR and pass after, however, I have only ensure the tests simply pass.

MQT Extraction (closes #527)

I have also included a module called nxcals with a file called mqt_extraction. In fairness, this should not have been in this PR, but both issues are MQT related. Please let me know if you would prefer this as a script or change the module name and/or usage, I am not compelled by anything.

Tests

The tests test that we get the same result as extractions previously done this year by the GUI. They pass on my machine, and I would recommend you running these, as they won't automatically be run by GitHub.

- Implemented a new module `mqt_extraction.py` for retrieving MQT (Quadrupole Trim) knob values from NXCALS for specified beams and timestamps.
- Added helper functions to generate MAD-X MQT variable names and retrieve corresponding values.
- Introduced `DateOrStr` class in `iotools.py` for flexible datetime and string parsing.
- Created sample output files for MQT extraction tests: `extracted_mqts_b1.str` and `extracted_mqts_b2.str`.
- Developed unit tests for MQT extraction functionality in `test_nxcals_mqt_extraction.py`, ensuring correct retrieval and formatting of MQT values.
- Added a utility script `generate_mqt_references.py` to copy reference MQT files from CERN data paths for testing purposes.
@jgray-19 jgray-19 requested a review from fsoubelet November 3, 2025 12:11
@jgray-19 jgray-19 self-assigned this Nov 3, 2025
@jgray-19 jgray-19 requested a review from a team as a code owner November 3, 2025 12:11
@jgray-19 jgray-19 added Type: Bug Something isn't working as it should. Type: Feature A (suggetion for a) new feature or enhancement in functionality. Priority: Medium Work on this. Status: Review Needed Work currently stopped, untils someone else reviews it. labels Nov 3, 2025
@jgray-19 jgray-19 moved this to In Progress in OMC ToDo 2025 Nov 3, 2025
@github-actions
Copy link

github-actions bot commented Nov 3, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  omc3
  knob_extractor.py 390-391, 506-519, 577
  mqt_extractor.py 157-178, 183-186, 190
  omc3/correction
  response_madx.py 322-323, 330-336
  sequence_evaluation.py 199, 220-222, 306
  omc3/model/accelerators
  lhc.py 176, 282-288, 295-296, 333, 353-357
  omc3/model/model_creators
  abstract_model_creator.py 96, 122, 130, 156, 400, 411, 530-537, 541
  lhc_model_creator.py 99, 178, 356-357, 452, 497, 587
  ps_model_creator.py 62, 69-70
  psbooster_model_creator.py
  sps_model_creator.py 70-72, 91, 94-104, 247
  omc3/nxcals
  __init__.py
  constants.py
  knob_extraction.py 91-142, 170-215, 232-236, 255-277, 293, 319-333
  mqt_extraction.py 42-47, 73-79, 92-99
  omc3/utils
  iotools.py 116-117, 134, 142, 244, 287-290
  time_tools.py 99-110, 139
Project Total  

This report was generated by python-coverage-comment-action

Comment on lines 462 to 514

# use absolute paths to force files into twiss_out directory instead of model-dir
self.jobfile = self.twiss_out.parent.absolute() / f"job.create_{self.twiss_out.stem}.madx"
self.logfile= self.twiss_out.parent.absolute() / f"job.create_{self.twiss_out.stem}.log"
self.logfile = self.twiss_out.parent.absolute() / f"job.create_{self.twiss_out.stem}.log"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the line 510 you can also use the _get_madx_path() and then skip the absolute() here?
Now comment: # Put job and logfile into the same dir as the corrected twiss, not the model-dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have a look at the new changes?

Comment on lines 263 to 269
state={
"action": 'store_true',
"help": (
"Prints the state of the StateTracker. "
"Does not extract anything else."
),
"action": "store_true",
"help": ("Prints the state of the StateTracker. Does not extract anything else."),
},
output={
"type": PathOrStr,
"help": (
"Specify user-defined output path. "
"This should probably be `model_dir/knobs.madx`"
"Specify user-defined output path. This should probably be `model_dir/knobs.madx`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no fmt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, either that or you should remove the () around the first help-string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

from datetime import datetime

# import jpype
from pyspark.sql import SparkSession
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still not in the requirements!

It's in the optional ones now, but that's not really clear from the code.

As it is a normal pypi package (not an acc-py-package) I'd just put it in the normal requirements? @fsoubelet?

The extraction uses the underlying `knob_extraction` module with MQT-specific patterns.
"""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotations

from omc3.model.manager import get_accelerator
from omc3.model.model_creators.lhc_model_creator import (
LhcBestKnowledgeCreator,
# LhcBestKnowledgeCreator,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need it or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No its' not needed if the EXTRACTED_MQTS_FILENAME stays moved, not sure if we reached consensus on that?

Comment on lines 512 to 518
# Take the directory of the twiss output as output dir
self.jobfile = self.resolve_madx_path(
self.twiss_out.parent / f"job.create_{self.twiss_out.stem}.madx"
)
self.logfile = self.resolve_madx_path(
self.twiss_out.parent / f"job.create_{self.twiss_out.stem}.log"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as twiss_out is already resovled, I don't think you need the function again for each of the sub-paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok, will test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: Medium Work on this. Status: Review Needed Work currently stopped, untils someone else reviews it. Type: Bug Something isn't working as it should. Type: Feature A (suggetion for a) new feature or enhancement in functionality.

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Bug]: LHC best knowledge model creation [Feature Request]: MQT Extraction

4 participants